#include <stdlib.h>

#include "linkedlist.h"
#include "linkedstack.h"

/*
 * Declaratii de structuri
 */

struct linkedstack_adt {
	LinkedList list;
	int count;
};

/*
 * Functii publice
 */

LinkedStack LS_New() {
	LinkedStack result;
	
	result = (LinkedStack) malloc(sizeof(struct linkedstack_adt));
	
	result->list = LL_New();
	result->count = 0;
	
	return result;	
}

void LS_Free(LinkedStack stack) {
	if (stack == NULL) 
		return;
	
	LL_Free(stack->list);
	
	free(stack);
}

int LS_Size(LinkedStack stack) {
	if (stack == NULL)
		return -1;
	
	return stack->count;
}

int LS_Empty(LinkedStack stack) {
	if (stack == NULL)
		return -1;
	
	return (stack->count == 0) ? 1 : 0;
}

void LS_Push(LinkedStack stack, void *el) {
	if (stack == NULL)
		return;
	
	LL_Add(stack->list, LL_Begin(stack->list), el);
	
	stack->count++;
}

void LS_Pop(LinkedStack stack) {
	if (stack == NULL || stack->count == 0)
		return;
	
	LL_Remove(stack->list, LL_Begin(stack->list));
	
	stack->count--;
}

void *LS_Top(LinkedStack stack) {
	if (stack == NULL || stack->count == 0)
		return NULL;
	
	return LL_Get(stack->list, LL_Begin(stack->list));
}
